﻿using System;
using Baby.GrowUp.Data;

namespace Baby.GrowUp.Model
{
    /// <summary>
    /// 相册
    /// </summary>
    public class Album
    {
        #region 属性
        /// <summary>
        /// 唯一编号
        /// </summary>
        [DataProperty("A_Id")]
        public string A_Id { get; set; }

        /// <summary>
        /// 相册名称
        /// </summary>
        [DataProperty("A_Title")]
        public string A_Title { get; set; }

        /// <summary>
        /// 相册说明
        /// </summary>
        [DataProperty("A_Detail")]
        public string A_Detail { get; set; }

        /// <summary>
        /// 封面照片
        /// </summary>
        [DataProperty("A_CoverPath")]
        public string A_CoverPath { get; set; }

        /// <summary>
        /// 是否公开
        /// </summary>
        [DataProperty("A_IsPrivate")]
        public bool A_IsPrivate { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [DataProperty("A_CreateDate")]
        public DateTime A_CreateDate { get; set; }

        /// <summary>
        /// 所属用户
        /// </summary>
        [DataProperty("U_Id")]
        public string U_Id { get; set; }
        #endregion

        #region 创建或更新相册
        /// <summary>
        /// 创建或更新相册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool CreateOrUpdate(Album model)
        {
            using (SqlDbConnection con = SqlDbConnection.Connect())
            {
                if (!string.IsNullOrWhiteSpace(model.A_Id))
                {
                    //更新
                    con.CommandText =
                        "Update Album Set A_Title = @A_Title, A_Detail = @A_Detail, A_Detail = @A_Detail, A_CoverPath = @A_CoverPath, A_IsPrivate = @A_IsPrivate, A_CreateDate = @A_CreateDate, U_Id = @U_Id where A_Id = @A_Id";
                }
                else
                {
                    //新增
                    con.CommandText =
                        "insert into Album (A_Id,A_Title,A_Detail,A_CoverPath,A_IsPrivate,A_CreateDate,U_Id)values (@A_Id,@A_Title,@A_Detail,@A_CoverPath,@A_IsPrivate,@A_CreateDate,@U_Id)";
                }
                con.AddParameters("A_Id", model.A_Id);
                con.AddParameters("A_Title", model.A_Title);
                con.AddParameters("A_Detail", model.A_Detail);
                con.AddParameters("A_CoverPath", model.A_CoverPath);
                con.AddParameters("A_IsPrivate", model.A_IsPrivate);
                con.AddParameters("A_CreateDate", model.A_CreateDate);
                con.AddParameters("U_Id", model.U_Id);

                return con.Exec() > 0;
            }
        }
        #endregion

        #region 获取所有自己的相册
        /// <summary>
        /// 获取所有自己的相册
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PageData QueryAlbumData(string uid, int pageIndex, int pageSize)
        {
            using (SqlDbConnection con = SqlDbConnection.Connect())
            {
                con.CommandText = "Select * from Album where U_Id = @uid";
                con.AddParameters("uid", uid);
                return con.Pageing(pageIndex, pageSize, "A_CreateDate", SqlDbConnection.OrderBy.DESC);
            }
        }
        #endregion

        #region 删除相册，并删除相册下所有的照片
        /// <summary>
        /// 删除相册，并删除相册下所有的照片
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static bool DeleteAlbum(string aid, string uid)
        {
            using (SqlDbConnection con = SqlDbConnection.Connect())
            {
                con.BeginTransaction();
                try
                {
                    con.CommandText = "delete from Album where A_Id = @aid and U_Id = @uid";
                    con.AddParameters("aid", aid);
                    con.AddParameters("uid", uid);
                    con.Exec();

                    con.CommandText = "delete from Photo where P_A_Id = @aid and U_Id = @uid";
                    con.AddParameters("aid", aid);
                    con.AddParameters("uid", uid);
                    con.Exec();
                    con.CommitTransaction();
                    return true;
                }
                catch
                {
                    con.RollbackTransaction();
                    return false;
                }
            }
        }
        #endregion

        #region 将某相册下的所有相片移动到另一个相册下
        /// <summary>
        /// 将某相册下的所有相片移动到另一个相册下
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="toAid"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static bool ResetPhotoToAlbum(string aid, string toAid, string uid)
        {
            using (SqlDbConnection con = SqlDbConnection.Connect())
            {
                con.CommandText = "select * from Album where A_Id = @aid and U_Id = @uid";
                con.AddParameters("aid", aid);
                con.AddParameters("uid", uid);
                Album resetAlbum = con.ToObject<Album>();

                if (resetAlbum == null) return false;

                con.CommandText = "select * from Album where A_Id = @aid and U_Id = @uid";
                con.AddParameters("aid", toAid);
                con.AddParameters("uid", uid);
                Album toAlbum = con.ToObject<Album>();

                if (toAlbum == null) return false;

                con.CommandText = "update Photo set P_A_Id = @to where P_A_Id = @re and U_Id = @uid";
                con.AddParameters("to", toAlbum);
                con.AddParameters("re", aid);
                con.AddParameters("uid", uid);
                return con.Exec() > 0;
            }
        }
        #endregion
    }
}
